﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace nr.InteractiveQuery
{
    public partial class FilterUserInput : UserControl
    {
        public event FilterInputTextChangedEventHandler ValueChanged;

        public class FieldsList : List<System.Collections.DictionaryEntry>
        {
            public FieldsList() : base() { }
        }

        public FieldsList Fields
        {
            set
            {
                cbField.Items.Clear();
                foreach (System.Collections.DictionaryEntry entry in value)
                    cbField.Items.Add(entry);
                cbField.DisplayMember = "Key";
                cbField.ValueMember = "Value";
                Clear();
            }
        }

        public string FilterText
        {
            get
            {
                return string.Format("{0} {1} [{2}]", ((System.Collections.DictionaryEntry)cbField.SelectedItem).Key, cbOperation.Items[cbOperation.SelectedIndex], txtValue.Text);
            }
        }

        public SimpleFilter Filter
        {
            get
            {
                SimpleFilter filter = null;
                string fieldName = ((System.Collections.DictionaryEntry)cbField.SelectedItem).Value.ToString();
                string value = txtValue.Text;
                switch (cbOperation.SelectedIndex)
                {
                    case 0: // ==
                        filter = new EQFilter(fieldName, value);
                        break;
                    case 1: // Starts With
                        filter = new RegExFilter(fieldName, string.Format("^{0}.+", value));
                        break;
                    case 2: // !=
                        filter = new NEFilter(fieldName, value);
                        break;
                    case 3: // <
                        filter = new LTFilter(fieldName, value);
                        break;
                    case 4: // >
                        filter = new GTFilter(fieldName, value);
                        break;
                    case 5: // <=
                        filter = new LEFilter(fieldName, value);
                        break;
                    case 6: // >= 
                        filter = new GEFilter(fieldName, value);
                        break;
                    case 7: // LIKE
                        filter = new RegExFilter(fieldName, value);
                        break;
                }
                return filter;
            }
        }

        public FilterUserInput()
        {
            InitializeComponent();
        }

        private void FilterUserInput_Load(object sender, EventArgs e)
        {
            Clear();
        }

        public void Clear()
        {
            if (cbField.Items.Count > 0)
                cbField.SelectedIndex = 0;
            cbOperation.SelectedIndex = 0;
            txtValue.SelectAll();
            txtValue.Focus();
        }

        private void txtValue_TextChanged(object sender, EventArgs e)
        {
            if (ValueChanged != null)
                ValueChanged(this, new FilterInputTextChangedEventArgs(txtValue.Text));
        }

        private void txtValue_Enter(object sender, EventArgs e)
        {
            txtValue.SelectAll();
        }
    }

    public delegate void FilterInputTextChangedEventHandler(object sender, FilterInputTextChangedEventArgs e);

    public class FilterInputTextChangedEventArgs : EventArgs
    {
        private string text;
        public string Text { get { return text; } }
        public FilterInputTextChangedEventArgs(string text)
            : base()
        {
            this.text = text;
        }
    }
}
